package com.yf.simpleadapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView lv_list = (ListView) findViewById(R.id.lv_list);
        
        String[] from = new String[]{"title", "content"};
		int[] to = {R.id.tv_title, R.id.tv_content};
		ArrayList<Map<String, String>> data = new ArrayList<Map<String, String>>();
		Map<String, String> item = new HashMap<String, String>();
		item.put("title", "中国足球再次冲击世界杯");
		item.put("content", "9月6日晚，在沈阳与伊朗0:0");
		data.add(item);
		
		Map<String, String> item2 = new HashMap<String, String>();
		item2.put("title", "北京科兴中维新冠疫苗口碑良好");
		item2.put("content", "北京科兴中维新冠疫苗需要接种二针，北京接种率达90%");
		data.add(item2);
		/**
		 * 1.上下文
		 * 2.要显示的数据，整个ListView的数据结构是一个List,List中的项对应ListView中的项，
		 * 	   每一项是一个k-v pair由from中元素作为key,要显示的文本作为value.
		 * 3.xml布局文件id
		 * 4.ListView中每一项要显示的文本在data的项中对应的key
		 * 5.展示文本控件id
		 */
		SimpleAdapter adapter = new SimpleAdapter(this, data , R.layout.item, from, to);
        lv_list.setAdapter(adapter);
    }
}
